package test.netty;

import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

public class NettyClientHandler extends ChannelInboundHandlerAdapter {
    private Channel channel;
    @Override
    public void channelActive(ChannelHandlerContext ctx) {
        // 连上后立即发一句
        this.channel = ctx.channel();
        ctx.writeAndFlush("Hello Netty Server!\n");
    }
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {

    }
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
    }
    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        ctx.flush();
    }
    public void sendMessageWithString(String msg) {
        channel.writeAndFlush(msg);
    }
    public void sendMessageWithBytes(byte[] msg) {
        channel.writeAndFlush(msg);
    }
    public void sendMessageWithInt(int msg) {
        channel.writeAndFlush(msg);
    }
}
